package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Account_Upload_Transfers_Table;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.model.RedeemCodeObject;
import com.warrantyhotel.android.service.UpdateIntentService;
import com.warrantyhotel.android.util.DetachableResultReceiver;
import com.warrantyhotel.android.util.DetachableResultReceiver.Receiver;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;
import com.warrantyhotel.android.util.ToastUtil;
import com.warrantyhotel.android.util.imLoader2.ImageLoader;

public class InboxUploadWarrantyDetail extends Activity implements AsyncQueryListener, Receiver {

	private TextView txtInboxAccountUploadTitle;
	private TextView txtInboxAccountUploadDescription;
	private TextView storeName;
	private TextView productName;
	private TextView responce;
	private ImageView txtInboxAccountUploadBanner;
	private DetachableResultReceiver receiver;
	private NotifyingAsyncQueryHandler mHandler;
	private String guid;
	private ImageLoader imLoader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inbox_account_upload_detail);

		receiver = new DetachableResultReceiver(new Handler());
		receiver.setReceiver(this);
		imLoader = new ImageLoader(this);

		txtInboxAccountUploadDescription = (TextView) findViewById(R.id.inbox_account_upload_description);
		txtInboxAccountUploadTitle = (TextView) findViewById(R.id.inbox_account_upload_title);
		txtInboxAccountUploadBanner = (ImageView) findViewById(R.id.inbox_account_upload_banner);
		storeName = (TextView) findViewById(R.id.store_name_account_upload_inbox);
		responce = (TextView) findViewById(R.id.claim_responce);
		productName = (TextView) findViewById(R.id.product_name_account_upload_inbox);
		guid = getIntent().getExtras().getString(Keys._guid);
		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		retrieveWarrantyTransferById(guid);

	}

	private void retrieveWarrantyTransferById(String guid) {
		String[] projectionNews = { Inbox_Account_Upload_Transfers_Table._ID, Inbox_Account_Upload_Transfers_Table.GUID,
				Inbox_Account_Upload_Transfers_Table.TEASER, Inbox_Account_Upload_Transfers_Table.TITLE, Inbox_Account_Upload_Transfers_Table.EXPIRATION_DATE,
				Inbox_Account_Upload_Transfers_Table.PRODUCTNAME, Inbox_Account_Upload_Transfers_Table.STORE_NAME,
				Inbox_Account_Upload_Transfers_Table.FILE_URL };
		mHandler.startQuery(QueriesCodes.INBOX_ACCOUNT_UPLOAD_TRANSFER, Inbox_Account_Upload_Transfers_Table.CONTENT_URI, projectionNews,
				Inbox_Account_Upload_Transfers_Table.GUID + " = '" + guid + "'", null, null);
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.INBOX_ACCOUNT_UPLOAD_TRANSFER:
			if (cursor != null) {
				startManagingCursor(cursor);
				if (cursor.moveToFirst()) {
					String teaser = cursor.getString(cursor.getColumnIndex(Inbox_Account_Upload_Transfers_Table.TEASER));
					String title = cursor.getString(cursor.getColumnIndex(Inbox_Account_Upload_Transfers_Table.TITLE));
					String store = cursor.getString(cursor.getColumnIndex(Inbox_Account_Upload_Transfers_Table.STORE_NAME));
					String product = cursor.getString(cursor.getColumnIndex(Inbox_Account_Upload_Transfers_Table.PRODUCTNAME));
					txtInboxAccountUploadTitle.setText(title);
					txtInboxAccountUploadDescription.setText(teaser);
					productName.setText(product);
					storeName.setText(store);
					String url_banner = cursor.getString(cursor.getColumnIndex(Inbox_Account_Upload_Transfers_Table.FILE_URL));

					imLoader.DisplayImage(url_banner, txtInboxAccountUploadBanner);

					// BitmapUtils.fetchImage(getApplicationContext(),
					// url_banner, new OnFetchCompleteListener() {
					//
					// @Override
					// public void onFetchComplete(Object cookie, Bitmap result)
					// {
					// txtInboxAccountUploadBanner.setImageBitmap(result);
					//
					// }
					// });
				}
			}
			break;
		}

	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case UpdateIntentService.STATUS_RUNNING:
			break;
		case UpdateIntentService.STATUS_ERROR:
			break;
		case UpdateIntentService.STATUS_FINISHED:
			if (resultData.containsKey(Keys.actionIdentificator) && resultData.getInt(Keys.actionIdentificator) == QueriesCodes.INBOX_INNER_WARRANTY_CLAIM) {
				RedeemCodeObject redeemCode = resultData.getParcelable(Keys._redeemCode);
				responce.setText(redeemCode.getMessage());
				ToastUtil.showText(getApplicationContext(), redeemCode.getMessage());

			} else if (resultData.containsKey(Keys.actionIdentificator)
					&& resultData.getInt(Keys.actionIdentificator) == QueriesCodes.INBOX_INNER_WARRANTY_REJECT) {
				RedeemCodeObject redeemCode = resultData.getParcelable(Keys._redeemCode);
				ToastUtil.showText(getApplicationContext(), redeemCode.getMessage());
				responce.setText(redeemCode.getMessage());
			}

			break;
		}

	}

	public void onClaim(View view) {
		Intent discardIntent = new Intent(Intent.ACTION_SYNC, null, InboxUploadWarrantyDetail.this, UpdateIntentService.class);
		discardIntent.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
		discardIntent.putExtra(Keys._account_upload_id, guid);
		discardIntent.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.INBOX_INNER_WARRANTY_CLAIM);
		startService(discardIntent);
	}

	public void onReject(View view) {
		Intent discardIntent = new Intent(Intent.ACTION_SYNC, null, InboxUploadWarrantyDetail.this, UpdateIntentService.class);
		discardIntent.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
		discardIntent.putExtra(Keys._account_upload_id, guid);
		discardIntent.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.INBOX_INNER_WARRANTY_REJECT);
		startService(discardIntent);
	}

}